#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QPushButton>
#include <qapplication.h>
#include <qapplication.h>
#include <QDesktopWidget>
#include "xyphonecontrol.h"
#include "xytablemodel.h"
#include <QApplication>
#include <QSettings>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->init();
    this->initBindSlot();
    this->initConf();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::init()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setFixedSize(this->size());
    m_pControl = new XyPhoneControl(this); 
   // ui->tableView->setModel(new XyTableModel(ui->tableView));
}

void Widget::initBindSlot()
{
    connect(ui->closeBtn,&QPushButton::clicked,this,&Widget::slot_close);
    connect(ui->btn_register,&QPushButton::clicked,this,&Widget::slot_sipRegister);
    connect(ui->btn_dial,&QPushButton::clicked,this,&Widget::slot_sipCall);
    connect(ui->btn_hangup,&QPushButton::clicked,this,&Widget::slot_sipHangup);
}

void Widget::initConf()
{
    QString xyInit = QApplication::applicationDirPath()+"/config.ini";
    QSettings xySet(xyInit,QSettings::IniFormat);
    xySet.setIniCodec("UTF-8");
    xySet.beginGroup("config");
    QStringList xyKeys = xySet.childKeys();
    for(QString key:xyKeys)
    {
        m_mapConf.insert(key,xySet.value(key));
    }
    xySet.endGroup();
}

void Widget::slot_close()
{
    this->close();
}

void Widget::slot_sipRegister()
{
    QString xyAccId = m_mapConf.value("accId").toString();
    QString xyAccName = m_mapConf.value("accName").toString();
    QString xyaccPasswd = m_mapConf.value("accPasswd").toString();
    QString xyaccAddr = m_mapConf.value("accAddr").toString();
    QString xyaccPort = m_mapConf.value("accPort").toString();
    QString xyaccProxyAddr = m_mapConf.value("accProxyAddr").toString();
    QString xyaccProxyPort = m_mapConf.value("accProxyPort").toString();
    QString xyaccDomain = m_mapConf.value("accDomain").toString();
    QVariantMap mapParam;
    mapParam.insert("sipAccountId",xyAccId);
    mapParam.insert("sipAccountName",xyAccName);
    mapParam.insert("sipPassword",xyaccPasswd);
    mapParam.insert("sipAddress",xyaccAddr);
    mapParam.insert("sipPort",xyaccPort);
    mapParam.insert("sipProxyAddress",xyaccProxyAddr);
    mapParam.insert("sipProxyPort",xyaccProxyPort);
    mapParam.insert("sipDomain",xyaccDomain);
    m_pControl->sipRegister(mapParam);
}

void Widget::slot_sipCall()
{
    QVariantMap mapParam;
    mapParam.insert("destNumber","1003");
    mapParam.insert("sipAddress",m_mapConf.value("accAddr"));
    m_pControl->sipCall(mapParam);
}

void Widget::slot_sipHangup()
{
    QVariantMap mapParam;
    m_pControl->sipHangup(mapParam);
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_last = QCursor::pos() - frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        QRect desktopRc = QApplication::desktop()->availableGeometry();
        QPoint curPoint = event->globalPos() - m_last;
        if (event->globalY() > desktopRc.height())
        {
            curPoint.setY(desktopRc.height() - m_last.y());
        }
        move(curPoint);
    }
    QWidget::mouseMoveEvent(event);
}
